package cn.zzp.user.controller;

import cn.zzp.basic.exception.BusinessException;
import cn.zzp.basic.util.JsonResult;
import cn.zzp.basic.util.PageList;
import cn.zzp.user.domain.User;
import cn.zzp.user.dto.UserDto;
import cn.zzp.user.query.UserQuery;
import cn.zzp.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserController
 * @Description
 * @Author 祝志鹏
 * @Date 2022/7/17 10:24
 */
@Api(tags = "普通用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<User> queryAll() {
        return userService.queryAll();
    }

    /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name="id", dataType="Long", required=true, value="用户Id")
    })
    @ApiOperation(value = "根据id查询一条数据" )
    @GetMapping("/{id}")
    public User queryOne(@PathVariable("id") Long id) {
        return userService.queryById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ApiOperation(value = "删除操作" )
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            userService.delete(id);
            return  JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 添加或修改
     * @param user
     * @return
     */
    @ApiOperation(value = "添加或修改" )
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody User user){
        try {
            if(user.getId() == null){
                userService.add(user);
            }else{
                userService.update(user);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 分页高级查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页高级查询" )
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery query){
        try {
            return userService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            userService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 手机注册
     * @param userDto
     * @return
     */
    @PostMapping("/phoneRegister")
    public JsonResult phoneRegister(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);

            return JsonResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return JsonResult.me("注册失败：" + e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }
}
